package com.woniu40.petai_manager.web;

import com.github.pagehelper.PageInfo;
import com.woniu40.petai_manager.entity.Article;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("article")
@Api(value = "知识星球的管理")
@CrossOrigin
public class ArticleController {
    @Autowired
    ArticleService articleService;

    @Value("${filepath}")
    private String uploadPath ;

    @GetMapping("/listAllArticle")
    @ApiOperation("分页查询知识星球的文章")
    @ApiImplicitParams({
        @ApiImplicitParam( name = "keyword" , required = false , dataType = "string" , paramType = "query"),
        @ApiImplicitParam( name = "pageNum" , required = false , dataType = "Integer" , paramType = "query"),
        @ApiImplicitParam( name = "pageSize" , required = false , dataType = "Integer" , paramType = "query")
    })
    public ResponseResult<PageInfo<Article>> listAllArticles(@RequestParam( value = "pageNum" , defaultValue = "1")Integer pageNum ,
                                                             @RequestParam( value = "pageSize" , defaultValue = "2")Integer pageSize ,
                                                             String keyword){
        System.out.println(pageSize+","+pageNum+","+keyword);
        PageInfo<Article> articleList = articleService.listArticles(pageNum,pageSize,keyword);

        ResponseResult<PageInfo<Article>> result = new ResponseResult<>();

        try{ result.setCode(200);
            result.setMessage("知识星球查询成功");
            result.setData(articleList);

            return result ;}catch(Exception e){ e.printStackTrace();result.setCode(107);result.setMessage("知识星球查询失败");return result;}
    }

    @PutMapping("/updateArticle")
    @ApiOperation("更新知识星球的文章")
    public ResponseResult<Article> updateArticle(@RequestBody Article modArticle){
//        Integer usercode = (Integer) session.getAttribute("Usercode");
//        modArticle.setManagerId(usercode);
        int i = articleService.updateArticle(modArticle);
        ResponseResult<Article> result = new ResponseResult<>();
        if (i == 1){
            result.setCode(200);
            result.setMessage("修改文章信息成功");
            return result;
        }else{
            result.setCode(108);
            result.setMessage("修改文章失败");
            return result;
        }
    }

    @DeleteMapping("/removeArticle")
    @ApiOperation("删除知识星球的文章")
    public ResponseResult<Article> removeArticle(Integer articleId){
        int i =  articleService.removeArticle(articleId);
        ResponseResult<Article> result = new ResponseResult<>();
        if (i == 1){
            result.setCode(200);
            result.setMessage("删除文章成功");
            return result;
        }else {
            result.setCode(109);
            result.setMessage("删除文章失败");
            return result;
        }

    }

    @PostMapping("/addArticle")
    @ApiOperation("在知识星球中添加文章")
    public ResponseResult<Article> addArticle(@RequestBody Article addArticle){
//        Integer usercode = (Integer) session.getAttribute("Usercode");
//        addArticle.setManagerId(usercode);
        int i = articleService.addArticle(addArticle);
        ResponseResult<Article> result = new ResponseResult<>();
        if (i == 1){
            result.setCode(200);
            result.setMessage("添加文章成功");
            return result;
        }else{
            result.setCode(110);
            result.setMessage("添加文章失败");
            return result;
        }
    }

    @RequestMapping("/uploadimgs")
    @ApiOperation("功能有Bug,请暂时不要测试")
    public ResponseResult<String> uploadimgs(@RequestParam("contentImgs") MultipartFile multipartFile) throws IOException {
        String fileName = multipartFile.getOriginalFilename();
        String path = uploadPath ;
        String baseName = "petai_"+System.currentTimeMillis();
        String subFix = fileName.substring(fileName.lastIndexOf("."));
        String newFileName = baseName + subFix;
        File file = new File(path + newFileName);
        multipartFile.transferTo(file);
        ResponseResult<String> result = new ResponseResult<>();
        result.setData(newFileName);
        result.setCode(200);
        result.setMessage("图片上传成功");
        return result;
    }


}
